import json
import unittest
import os
from Server import app
import shutil


class MyTestCase(unittest.TestCase):
    def test_file_reading(self):
        relpath = os.path.normpath('./Data/Example.txt')
        st = os.stat(relpath)
        info = {}
        path, name = os.path.split(relpath)
        info['path'] = path
        info['name'] = name
        info['mtime'] = st.st_mtime
        info['type'] = 'dir' if os.path.isdir(relpath) else 'file'
        info['size'] = st.st_size
        print(info)

    def test_User_file(self):
        relpath = os.path.normpath('c:\\Users\\yuang\\Application Data')
        st = os.stat(relpath)
        info = {}
        path, name = os.path.split(relpath)
        info['path'] = path
        info['name'] = name
        info['mtime'] = st.st_mtime
        info['type'] = 'dir' if os.path.isdir(relpath) else 'file'
        info['size'] = st.st_size
        # subfiles = os.listdir(relpath)
        os.path.isdir(relpath)
        print(info)


class APITestCase(unittest.TestCase):
    base_dir = os.path.abspath('.')

    def setUp(self, basedir=base_dir) -> None:
        tmp_path = os.path.join(basedir,'Data','tmp')
        self.client = app.test_client()
        shutil.rmtree(tmp_path)
        os.mkdir(tmp_path)

    def test_echo(self):
        response = self.client.get('/api/echo', data=b'1yg23')
        print(response.data)

    def test_file_download(self, basedir=base_dir):
        print(basedir)
        file_path = os.path.join(basedir, 'Data', 'target.exe')
        response = self.client.get('/api/download', headers={
            'FILE_PATH': file_path
        })
        self.assertEqual("200 OK",response.status)
        with open(file_path,'rb') as file:
            data = file.read()
        self.assertEqual(response.data,data)

    def test_path_right(self,basedir = base_dir):
        file_path = os.path.join(basedir,'Data','target.exe')
        target_path = os.path.join(basedir,'Data','tmp','target.exe')
        with open(file_path,'rb') as f:
            data = f.read()
        with open(target_path,'wb') as f:
            f.write(data)

    def test_file_upload(self,basedir = base_dir):
        file_path = os.path.join(basedir,'Data','target.exe')
        target_path = os.path.join(basedir,'Data','tmp','target.exe')
        with open(file_path,'rb') as f:
            data = f.read()
        response = self.client.get(
            '/api/upload',# "http://localhost:5000/api/upload"
            headers={
                'FILE_PATH':target_path
            },
            data=data
        )
        self.assertEqual('200 OK',response.status)
        self.assertEqual(response.data,b'OK')
        with open(target_path,'rb') as file:
            data_past = file.read()
        self.assertEqual(data,data_past)

    def test_file_status(self,basedir = base_dir):
        from Server.blueprint.api import get_status
        file_path = os.path.join(basedir,'Data','target.exe')
        local_status = get_status(file_path)
        response = self.client.get(
            '/api/file_status',
            headers = {
                'FILE_PATH':file_path,
            },
        )
        self.assertEqual('200 OK',response.status)
        remote_status = json.loads(response.data.decode())
        self.assertEqual(local_status,remote_status)

    def test_folder_status(self,basedir = base_dir):
        from Server.blueprint.api import get_status
        dirpath = os.path.join(basedir,'Data')
        folder_info = [get_status(os.path.join(dirpath,filename)) for filename in os.listdir(dirpath)]
        response = self.client.get(
            '/api/folder_status',
            headers = {
                'FOLDER_PATH':dirpath
            },
        )
        self.assertEqual('200 OK',response.status)
        remote_status = json.loads(response.data.decode())
        print(response.data)
        self.assertEqual(folder_info,remote_status)


if __name__ == '__main__':
    unittest.main()
